<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('三会管理') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('申请标题')" prop="actTitle">
        <el-input v-model="queryParams.actTitle" :placeholder="$t('申请标题')" size="small" />
      </el-form-item>
      <el-form-item :label="$t('会议类型')" prop="meetingType">
        <el-select v-model="queryParams.meetingType" :placeholder="$t('会议类型')" clearable size="small">
          <el-option
            v-for="dict in meetingTypeOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('会议时间')" prop="meetingTime">
        <DatePicker v-model="meetingTimes" type="daterange" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:meeting:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:meeting:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t('导出') }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="dealMeetingList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('申请标题')" align="center" prop="actTitle" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span class="link-type" @click="updateHandle(scope.row)">{{ scope.row.actTitle }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('会议类型')" align="center" prop="meetingType" :formatter="meetingTypeFormat" show-overflow-tooltip width="100" />
      <el-table-column :label="$t('会议时间')" align="center" prop="meetingTime" show-overflow-tooltip width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.meetingTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('我方参会人员')" show-overflow-tooltip align="center" prop="ourParticipants">
        <template slot-scope="scope">
          <span
            v-for="(ourParticipant,index) in scope.row.ourParticipantList"
            :key="index"
          >{{ ourParticipant.nickName }}{{ index === scope.row.ourParticipantList.length - 1 ? '' : ',' }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('其他参会人员')" align="center" prop="otherParticipants" show-overflow-tooltip />
      <el-table-column :label="$t('会议议题')" align="center" prop="ourParticipants" min-width="150" class-name="dealContractPlan" show-overflow-tooltip>
        <template slot-scope="scope">
          <div class="plan">
            <div v-for="(value,index) in scope.row.dealMeetingBillList" :key="index">
              <span>{{ value.topicsContent }}</span>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column :label="$t('会议决议')" align="center" prop="ourParticipants" min-width="80" class-name="dealContractPlan">
        <template slot-scope="scope">
          <div class="plan">
            <div v-for="(value,index) in scope.row.dealMeetingBillList" :key="index">
              <span>{{ formatTOurVote(value) }}</span>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column v-if="useProcess" :label="$t('审批状态')" align="center" width="120">
        <template slot-scope="scope">
          <ProcessStatus :status="scope.row.actStatus" :act-id="scope.row.actId" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="220">
        <template slot-scope="scope">
          <span style="display: none">{{ useProcess }}</span>
          <!-- 启动流程，终止流程，修改，删除 四个按钮 -->
          <ListBtn v-if="useProcess" :bus-data="scope.row" has-permi="deal:meeting" />
          <el-button
            v-if="!useProcess"
            v-hasPermi="['deal:meeting:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-if="!useProcess"
            v-hasPermi="['deal:meeting:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
          <el-button
            v-if="scope.row.actStatus === 'W'"
            size="small"
            type="text"
            icon="el-icon-s-promotion"
            @click="billMaintain(scope.row)"
          >{{ $t('维护最终结论') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改三会管理对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealMeetingForm ref="dealMeetingForm" />
    </el-dialog>

    <!-- 维护最终结论 -->
    <el-dialog :title="$t('维护最终结论')" :visible="billFormOpen" :close-on-click-modal="false" :lock-scroll="false" append-to-body @close="billFormOpen = false">
      <div v-loading="billLoading">
        <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
          <el-row>
            <el-col :span="12">
              <el-form-item :label="$t('会议时间')" prop="meetingTime">
                <DatePicker v-model="form.meetingTime" :placeholder="$t('会议时间')" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-table :data="form.dealMeetingBillList" border class="nowrapTable formTable">
            <el-table-column :label="$t('议题内容')" align="center" prop="topicsContent">
              <template slot="header" slot-scope="{}">
                <i style="color: #ff4949;margin-right: 4px;">*</i>
                <span>{{ $t('议题内容') }}</span>
              </template>
              <template slot-scope="scope">
                <el-input v-model="scope.row.topicsContent" :disabled="true" type="textarea" :autosize="{ maxRows: 5 }" :maxlength="2000" show-word-limit :placeholder="请输入" />
              </template>
            </el-table-column>
            <el-table-column :label="$t('我方表决')" align="center" prop="ourVote">
              <template slot="header" slot-scope="{}">
                <i style="color: #ff4949;margin-right: 4px;">*</i>
                <span>{{ $t('我方表决') }}</span>
              </template>
              <template slot-scope="scope">
                <dict-select
                  v-model="scope.row.ourVote"
                  dictionary="bill_vote"
                  :disabled="true"
                />
              </template>
            </el-table-column>
            <el-table-column :label="$t('说明')" align="center" prop="remark">
              <template slot-scope="scope">
                <el-input v-model="scope.row.remark" type="textarea" :autosize="{ maxRows: 5 }" :disabled="true" :maxlength="2000" show-word-limit :placeholder="请输入" />
              </template>
            </el-table-column>
            <el-table-column :label="$t('最终结论')" align="center" prop="finalConclusion">
              <template slot-scope="scope">
                <dict-select
                  v-model="scope.row.finalConclusion"
                  dictionary="final_ourVote"
                />
              </template>
            </el-table-column>
          </el-table>
        </el-form>
      </div>

      <div slot="footer" class="dialog-footer" style="text-align: right;">
        <el-button @click="billFormOpen = false">{{ $t("取消") }}</el-button>
        <save-btn has-permi="deal:meeting" :function="saveBill" :cancel-show="false" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDealMeeting,
  delDealMeeting,
  getDealMeeting, saveDealMeeting
} from '@/api/deal/meeting/dealMeeting'
import dealMeetingForm from './dealMeetingForm.vue'
import ProcessStatus from '@/components/Act/ProcessStatus/index'
import { getMenuProcess } from '@/api/act/process/wfMenuProcess'
export default {
    components: {
        dealMeetingForm,
        ProcessStatus
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          billLoading: false,
          // 总条数
          total: 0,
          // 三会管理表格数据
          dealMeetingList: [],
          ourVoteOptions: [],
          // 会议时间参数
          meetingTimes: undefined,
          // 弹出层标题
          title: '三会管理',
          // 是否显示弹出层
          formOpen: false,
         billFormOpen: false,
          // 会议类型 字典meeting_type字典
          meetingTypeOptions: [],
        useProcess: false,
          // 查询参数
          queryParams: {
              actTitle: undefined,
              companyId: this.$route.query.companyId,
              meetingType: undefined,
              startDate: undefined,
              endDate: undefined,
              pageNum: 1,
              pageSize: 10
          },
        // 议案列表
        dealMeetingBillList: [],
          // 表单参数
          form: {},
          // 表单校验
          rules: {
            finalConclusion: [
              { required: true, message: this.$t('必填信息'), trigger: 'blur' }
            ]
          },
        // 保存案例参数
        saveBillParam: {
            meetingId: undefined,
            list: []
        }
      }
  },
  created() {
      this.getUseProcess()
      this.getList()
      this.getDicts('meeting_type').then(response => {
          this.meetingTypeOptions = response.data
      })
      this.getDicts('final_ourVote').then(response => {
      this.ourVoteOptions = response.data
      })
  },
  methods: {
    /** 查询三会管理列表 */
    getList() {
        this.loading = true
        if (this.meetingTimes != null && this.meetingTimes.length > 1) {
          this.queryParams.startDate = this.meetingTimes[0]
          this.queryParams.endDate = this.meetingTimes[1]
        } else {
          this.queryParams.startDate = undefined
          this.queryParams.endDate = undefined
        }
        listDealMeeting(this.queryParams).then(response => {
            this.dealMeetingList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 会议类型 字典meeting_type字典翻译
    meetingTypeFormat(row) {
      return this.selectDictLabel(this.meetingTypeOptions, row.meetingType)
    },
    formatTOurVote(row) {
    return this.selectDictLabel(this.ourVoteOptions, row.finalConclusion)
    },
    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            meetingType: undefined,
            meetingTime: undefined,
            ourParticipants: undefined,
            otherParticipants: undefined,
            meetingIssue: undefined,
            meetingDecision: undefined,
            actId: undefined,
            actStatus: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.meetingTimes = undefined
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.dealMeetingForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.dealMeetingForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delDealMeeting(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        this.downLoadExcel('/meeting/dealMeeting/export', this.queryParams)
    },
    // 保存议案列表
    saveBill() {
      this.billLoading = true
      // this.saveBillParam.list = this.dealMeetingBillList
      saveDealMeeting(this.form).then(response => {
        if (response.code === 200) {
          this.msgSuccess(this.$t('保存成功'))
          this.billFormOpen = false
          this.billLoading = false
          this.getList()
        }
      })
    },
    // 维护最终结果
    billMaintain(row) {
      this.billFormOpen = true
      this.billLoading = true
      this.saveBillParam.meetingId = row.id
      getDealMeeting(row.id).then(response => {
        this.form = response.data
        this.billLoading = false
      })
      // getByMeetingId(row.id).then(response => {
      //   this.dealMeetingBillList = response.data
      //   this.billLoading = false
      // })
    },
    // 获取是否使用流程
    getUseProcess() {
      getMenuProcess('deal_meeting').then(response => {
        this.useProcess = !!response.data
      })
    }
  }
}
</script>
